package com.ruoyi.system.aikeService;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.apiResult.ApiRe;
import com.ruoyi.common.apiResult.ChannelSupervise;
import com.ruoyi.common.apiResult.ZhtcRe;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.system.apiService.OrderService;
import com.ruoyi.system.apiService.ZhtcService;
import com.ruoyi.system.domain.ZsyyChannelInfo;
import com.ruoyi.system.domain.ZsyyOutPark;
import com.ruoyi.system.domain.ZsyyParkEnter;
import com.ruoyi.system.domain.ZsyyParkLeave;
import com.ruoyi.system.dto.OrderProductListDTO;
import com.ruoyi.system.mapper.ZsyyChannelInfoMapper;
import com.ruoyi.system.mapper.ZsyyOutParkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class HomeZhtcApiService   extends BaseService {
	public static HomeZhtcApiService proxy;
	@PostConstruct
	public void init() {
		proxy = this;
	}

	@Autowired
	private OrderService orderService;
	@Autowired
	private ZsyyChannelInfoMapper channelInfoMapper;

	@Autowired
	private ZhtcService zhtcService;

	@Autowired
	private ZsyyOutParkMapper outParkMapper;


	//缴费查询
	public String  getParkFee(String data, Map addParam) {
		try {
			long userId = Long.parseLong(addParam.get("userId").toString());
			Map reM = new HashMap();
			ApiRe re = new ApiRe();
			Map m = jsonToMap(data);
			reM.put("zhtcData",m);
			if(Integer.parseInt(m.get("status").toString())!=1){
				re.setData(reM);
				re.setCode(400);
				re.setMsg("缴费查询错误");
				return toJson(re) ;
			}
			Map dataM = (Map)m.get("data");
			ZsyyParkEnter peParam = new ZsyyParkEnter();
			peParam.setParkingCode(dataM.get("parkingCode").toString());
			ZsyyParkEnter pe = zhtcService.getParkEnter(peParam);
			dataM.put("carCode",pe.getCarLicenseNumber());
			OrderProductListDTO order = new OrderProductListDTO();
			int price = (int)(Double.parseDouble(dataM.get("shouldPayValue").toString())*100);
			if(price<=0){
				re.setCode(400);
				re.setMsg("您目前需缴费金额为零");
				return toJson(re) ;
			}
			order.setType(3);
			order.setRemark(JSON.toJSONString(dataM));
			order.setPrice(price);
			order.setProductName("停车缴费");
			order.setActualPrice(price);
			order.setOrderTag(dataM.get("parkingCode").toString());
			//添加订单
			re = orderService.submitOrder(order,userId);
			//添加离场
			ZsyyParkLeave ple = new ZsyyParkLeave();
			ple.setUserId(userId);
			ple.setParkingCode(dataM.get("parkingCode").toString());
			ple.setParkCode(dataM.get("parkCode").toString());
			zhtcService.addParkLeave(ple);

			reM.put("data",re.getData());
			re.setData(reM);

			return toJson(re) ;
		}catch (Exception e){
			return toJson(new ApiRe(1));
		}
	}




	//进场上报

	public String  reportCarIn( String data,Map addParam) {
		try {
			long userId = Long.parseLong(addParam.get("userId").toString());
			ZhtcRe re = new ZhtcRe();
			ZsyyParkEnter pe = JSON.parseObject(data,ZsyyParkEnter.class);

			//监控联动
			if(pe.getParkCode()!=null&&pe.getEnterChannelCode()!=null){
				String url = zhtcService.getStreamUrl(pe.getEnterChannelCode());
				if(url!=null){
					ChannelSupervise.uriSend("/websocket/park/"+pe.getParkCode(),url);
				}
			}
			if(userId>0){
				pe.setUserId(userId);
			}
			Map dataM = jsonToMap1(data);
			if(dataM.get("extendInfo")!=null&&!"".equals(dataM.get("extendInfo"))){
				Map extendInfo = (Map)dataM.get("extendInfo");
				pe.setSelfInRandomCode((String)extendInfo.get("selfInCode"));
				if(!StringUtils.isEmpty(extendInfo.get("selfInUserUniqCode"))){
					pe.setUserUniqCode(extendInfo.get("selfInUserUniqCode").toString());
				}
			}
			zhtcService.addParkEnter(pe);
			ZsyyOutPark outPark = new ZsyyOutPark();
//			outPark.setParkCode(pe.getParkCode());
			LambdaQueryWrapper<ZsyyOutPark> lqw = Wrappers.lambdaQuery();
			lqw.eq(ZsyyOutPark::getParkCode,pe.getParkCode());
			outPark = outParkMapper.selectOne(lqw);
			if(outPark!=null){

			}
			return toJson(re) ;
		}catch (Exception e){
			return toJson(new ZhtcRe(0));
		}
	}


	//进场上报
//    @PostMapping("/reportCarOut")
//    @ApiResponses({@ApiResponse(code = 200,message = "成功",response = WebsocketMsg.class)})
//    public @ResponseBody
	public String  reportCarOut( String data, Map addParam) {
//        , Map addParam
		try {
			long userId = Long.parseLong(addParam.get("userId").toString());
//            long userId = 0;
			ZhtcRe re = new ZhtcRe();
			ZsyyParkLeave pe = JSON.parseObject(data,ZsyyParkLeave.class);

			//监控联动
			if(pe.getParkCode()!=null&&pe.getLeaveChannelCode()!=null){
				String url = zhtcService.getStreamUrl(pe.getLeaveChannelCode());
				if(url!=null){
					ChannelSupervise.uriSend("/websocket/park/"+pe.getParkCode(),url);
				}
			}
			if(userId>0){
				pe.setUserId(userId);
			}
			zhtcService.addParkLeave(pe);

			return toJson(re) ;
		}catch (Exception e){
			return toJson(new ZhtcRe(0));
		}
	}

	//预进场上报
	public String  reportPreCarIn( String data,Map addParam) {
		try {
			long userId = Long.parseLong(addParam.get("userId").toString());
			ZhtcRe re = new ZhtcRe();
			ZsyyParkEnter pe = JSON.parseObject(data,ZsyyParkEnter.class);

			return toJson(re) ;
		}catch (Exception e){
			return toJson(new ZhtcRe(0));
		}
	}


	//预离场上报
	public String  reportPreCarOut( String data,Map addParam) {
		try {
			long userId = Long.parseLong(addParam.get("userId").toString());
			ZhtcRe re = new ZhtcRe();
			ZsyyParkLeave pe = JSON.parseObject(data,ZsyyParkLeave.class);
			zhtcService.addParkLeave(pe);

			Map dataM = jsonToMap1(data);
			zhtcService.isOpenAutoPay(dataM);
			return toJson(re) ;
		}catch (Exception e){
			return toJson(new ZhtcRe(0));
		}
	}


	//临时缴费上报
	public String  reportParkingFee( String data,Map addParam) {
		try {
			long userId = Long.parseLong(addParam.get("userId").toString());
			Map reM = new HashMap();
			ZhtcRe re = new ZhtcRe();
			Map m = jsonToMap(data);
			reM.put("zhtcData",m);
			Map dataM = (Map)m.get("data");
			re.setData(reM);

			return toJson(re) ;
		}catch (Exception e){
			return toJson(new ZhtcRe(0));
		}
	}

	//扫通道二维码自助进出场
	public String  scanChannelCodeInOut( String data,Map addParam) {
		try {
			long userId = Long.parseLong(addParam.get("userId").toString());
			Map reM = new HashMap();
			ZhtcRe re = new ZhtcRe();
			Map m = jsonToMap(data);
			reM.put("zhtcData",m);
			Map dataM = (Map)m.get("data");
			ZsyyParkEnter pe = new ZsyyParkEnter();
			if((int)m.get("resultCode")==0&&dataM.get("passType")!=null&&(int)dataM.get("passType")==1&& !StringUtils.isEmpty((String)dataM.get("selfInRandomCode"))){
				pe.setSelfInRandomCode(dataM.get("selfInRandomCode").toString());
				pe.setParkCode(dataM.get("parkCode").toString());
				pe.setCarLicenseNumber((String)addParam.get("carLicenseNumber"));
				if(userId>0){
					pe.setUserId(userId);
				}
				zhtcService.addParkEnter(pe);
			}
//            re.setData(reM);

			return data ;
		}catch (Exception e){
			return toJson(new ZhtcRe(0));
		}
	}

	//获取通道信息
	public String  getChannelInfo ( String data,Map addParam) {
		try {
			Map reM = new HashMap();
			ZhtcRe re = new ZhtcRe();
			Map m = jsonToMap(data);
			reM.put("zhtcData",m);
			Map dataM = (Map)m.get("data");
			List<ZsyyChannelInfo> list = JSON.parseArray(JSON.toJSONString(dataM.get("recordList")),ZsyyChannelInfo.class);
			for (ZsyyChannelInfo cha:list
			) {
				cha.setChannelCode(cha.getId());
				cha.setId(null);
				channelInfoMapper.insert(cha);
			}

			return data ;
		}catch (Exception e){
			return toJson(new ZhtcRe(0));
		}
	}
}
